package com.example.mediatest

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.widget.VideoView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mediatest.databinding.ActivityMainBinding
import androidx.core.net.toUri

class MainActivity : AppCompatActivity() {

    private val mediaPlayer = MediaPlayer()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // play
        AudioTest(binding)

        // video
        videoTest(binding)

    }

    private fun videoTest(binding: ActivityMainBinding) {
        val uri = "android.resource://$packageName/${R.raw.video}".toUri()
        binding.videoView.setVideoURI(uri)
        binding.apply {
            play.setOnClickListener {
                if (!videoView.isPlaying) {
                    videoView.start()
                }
            }

            pause.setOnClickListener {
                videoView.let {
                    if (it.isPlaying)
                        it.pause()
                }
            }

            stop.setOnClickListener {
                videoView.let {
                    if (it.isPlaying) {
                        it.resume()
                    }
                }
            }
        }
    }

    private fun AudioTest(binding: ActivityMainBinding) {

        initMediaPlayer()

        binding.duration.text = mediaPlayer.duration.toString()

        binding.playButton.setOnClickListener {
            if (!mediaPlayer.isPlaying)
                mediaPlayer.start()
        }
        // pause
        binding.pauseButton.setOnClickListener {
            if (mediaPlayer.isPlaying)
                mediaPlayer.pause()
        }

        // stop
        binding.stopButton.setOnClickListener {
            if (mediaPlayer.isPlaying) {
                mediaPlayer.reset()
                initMediaPlayer()
            }
        }

        // timestamp
        binding.button.setOnClickListener {
            binding.timeStamp.text = mediaPlayer.currentPosition.toString()
        }
    }

    private fun initMediaPlayer() {
        val assetManager = assets
        val fd = assetManager.openFd("bleep.mp3")
        mediaPlayer.setDataSource(fd.fileDescriptor, fd.startOffset, fd.length)
        mediaPlayer.prepare()
    }

    override fun onDestroy() {
        super.onDestroy()
        mediaPlayer.stop()
        mediaPlayer.release()

        val videoView = findViewById<VideoView>(R.id.videoView)
        videoView.suspend()
    }
}